﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SPFluid.Core.Validation.Extensions
{
    public static class EnumerableValidatorExtensions
    {
        public static IGuard<IEnumerable<T>> IsNotEmpty<T>(this IGuard<IEnumerable<T>> guard)
        {
            if (!guard.Argument.Value.Any())
            {
                guard.ExceptionHandler.ThrowArgumentException("Array is empty");
            }

            return guard;
        }

        public static IGuard<IEnumerable<T>> Contains<T>(this IGuard<IEnumerable<T>> guard, Func<T, bool> predicate)
        {
            if (!guard.Argument.Value.Any(predicate))
            {
                guard.ExceptionHandler.ThrowArgumentException("Collection does not contain required object");
            }

            return guard;
        }

        public static IGuard<IEnumerable<T>> CountIs<T>(this IGuard<IEnumerable<T>> guard, int count)
        {
            if (guard.Argument.Value.Count() != count)
            {
                guard.ExceptionHandler.ThrowArgumentOutOfRangeException();
            }

            return guard;
        }
    }
}